#include "..\examples\src\shared\gltools.h"

void RenderScene()
{
	glClear(GL_COLOR_BUFFER_BIT);

	glStencilFunc(GL_NOTEQUAL, 0x1, 0x1);
	glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);

	glColor3f(0.0f, 1.0f, 0.0f);

	glRectf(-20, 20, 20, -20);

	glutSwapBuffers();
}

void ChangeSize(GLsizei w, GLsizei h)
{
	GLfloat nRange = 100.0f;
	if (h == 0)
		h = 1;

	GLfloat aspectRatio = static_cast<GLfloat>(w) / static_cast<GLfloat>(h);

	glViewport(0, 0, w, h);

	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();

	if (w <= h)
	{
		glOrtho(-nRange, nRange, -nRange / aspectRatio, nRange / aspectRatio, -nRange, nRange);
	}
	else
	{
		glOrtho(-nRange * aspectRatio, nRange * aspectRatio, -nRange, nRange, -nRange, nRange);
	}

	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
}

void SetupRC()
{
	glClearStencil(0);
	glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
	
	glEnable(GL_STENCIL_TEST);

	glClear(GL_COLOR_BUFFER_BIT | GL_STENCIL);
	
	glStencilFunc(GL_NEVER, 0, 0);
	glStencilOp(GL_INCR, GL_INCR, GL_INCR);

	glColor3f(1.0f, 1.0f, 1.0f);

	GLfloat angle = 0.0f;
	GLfloat r = 0.0f;
	glBegin(GL_LINE_STRIP);
		
	for (;angle < 400.0f; angle += 0.1)
	{
		glVertex2d(r * cos(angle), r * sin(angle));
		r += 0.001;
	}

	glEnd();
}

int main(int argc, char * argv[])
{
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_STENCIL);
	glutInitWindowSize(800, 600);
	glutCreateWindow("stencil");
	glutDisplayFunc(RenderScene);
	glutReshapeFunc(ChangeSize);

	SetupRC();
	glutMainLoop();

	return 0;
}